#include <stdio.h>

// 定义电梯状态枚举
typedef enum {
    IDLE,
    MOVING_UP,
    MOVING_DOWN,
    DOOR_OPENING,
    DOOR_CLOSING
} ElevatorState;

// 状态转换函数
ElevatorState transition(ElevatorState currentState, int floorRequest, int currentFloor, int doorSensor) {
    switch (currentState) {
        case IDLE:
            return (floorRequest > currentFloor) ? MOVING_UP :
                   (floorRequest < currentFloor) ? MOVING_DOWN : IDLE;
        case MOVING_UP:
            return (currentFloor == floorRequest) ? DOOR_OPENING : MOVING_UP;
        case MOVING_DOWN:
            return (currentFloor == floorRequest) ? DOOR_OPENING : MOVING_DOWN;
        case DOOR_OPENING:
            return (doorSensor == 1) ? DOOR_CLOSING : DOOR_OPENING;
        case DOOR_CLOSING:
            if (doorSensor == 0) {
                return (floorRequest > currentFloor) ? MOVING_UP :
                       (floorRequest < currentFloor) ? MOVING_DOWN : IDLE;
            }
            return DOOR_CLOSING;
    }
    return currentState;
}

int main() {
    ElevatorState state = IDLE;
    int currentFloor = 1;
    int floorRequest = 0;
    int doorSensor = 0;

    while (1) {
        // 显示当前状态
        printf("当前状态：%s\n", 
               state == IDLE ? "空闲" : 
               state == MOVING_UP ? "上升中" : 
               state == MOVING_DOWN ? "下降中" : 
               state == DOOR_OPENING ? "门正在打开" : 
               "门正在关闭");

        // 显示当前楼层
        printf("当前楼层：%d\n", currentFloor);

        // 模拟楼层请求和门传感器信号输入
        printf("输入楼层请求（0 表示无请求）：");
        scanf("%d", &floorRequest);
        printf("输入门传感器信号（0 表示门关闭，1 表示门打开）：");
        scanf("%d", &doorSensor);

        // 根据状态更新楼层
        if (state == MOVING_UP) {
            currentFloor++;
        } else if (state == MOVING_DOWN) {
            currentFloor--;
        }

        // 更新电梯状态
        state = transition(state, floorRequest, currentFloor, doorSensor);
    }

    return 0;
}
